package apis

import (
	"xiaoercaidan/publicResources/controller"
	"xiaoercaidan/publicResources/models"
	"xiaoercaidan/baseApi/dao"
	"encoding/json"
	"github.com/goinggo/mapstructure"
	"xiaoercaidan/publicResources/utils"
)

type RingApi struct {
	controller.BaseController
}





// @Title GetRingList
// @Description 服务列表
// @Param shopId  patch string   true string       "店鋪ID"
// @Param cateId  patch string   true string       "分类ID"
// @router /ringList [get]
func (c *RingApi) GetRingList() {

	var BaseResult = new(models.BaseResult)

	var shopId = c.GetString("shopId")
	var cateId = c.GetString("cateId")

	if ( len(shopId) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "shopId不能为空"
	} else {

		var params = make(map[string]string)

		params["shop_id"] = shopId

		if len(cateId) > 0 {
			params["cate_id"] = cateId
		}

		rings := dao.QeryRing(params)

		BaseResult.Data = rings
	}

	c.Data["json"] = BaseResult

	c.ServeJSON()

}



// @Title DelRing
// @Description 刪除服务
// @Param shopId  patch string   true string       "店鋪ID"
// @Param   ids     patch string   true int64       "服务ID列表，以'，'分割"
// @router /ringDel [get]
func (this *RingApi) DelRing() {

	var BaseResult = new(models.BaseResult)

	var idstr = this.GetString("ids")
	var shopId = this.GetString("shopId")

	if ( len(shopId) <= 0) {

		BaseResult.Code = 100
		BaseResult.Msg = "shopId不能为空"

	} else if ( len(idstr) <= 0) {

		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"

	} else {

		err := dao.DeleteRing(shopId, idstr)

		if (err != nil) {
			BaseResult.Code = 100
			BaseResult.Msg = err.Error()
		} else {
			BaseResult.Msg = "删除成功"
		}

	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()
}



// @Title AddRing
// @Description 新增服务
// @Param   ring     patch string   true string       "呼叫json"
// @router /add [get]
func (c *RingApi) AddRing() {

	var BaseResult = new(models.BaseResult)

	var ringStr = c.GetString("ring")

	if ( len(ringStr) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"
	} else {

		var mapStr map[string]interface{}

		json.Unmarshal([]byte(ringStr), &mapStr)

		if (mapStr["Url"] == nil) {
			BaseResult.Code = 100
			BaseResult.Msg = "呼叫Url不能为空"
		} else {
			for k := range mapStr {
				if k == "Id" {
					mapStr[k] = nil
				}
			}

			var ring = new(models.Ring)

			mapstructure.Decode(mapStr, ring);


			if err := dao.InserRing(ring); (utils.CheckResult(BaseResult, err)) {
				BaseResult.Data = ring
			}
		}

	}
	c.Data["json"] = BaseResult

	c.ServeJSON()

}


// @Title RingUpdate
// @Description  服务修改
// @Param   ring     patch string   true string       "呼叫json"
// @router /ringUpdate [get]
func (this *RingApi) RingUpdate() {

	var BaseResult = new(models.BaseResult)
	var ring = new(models.Ring)

	var ringJson = this.GetString("ring")

	if ( len(ringJson) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数不能为空"
	} else {

		json.Unmarshal([]byte(ringJson), &ring)

		err := dao.UpdateRing(ring)

		if (utils.CheckResult(BaseResult, err)) {
			BaseResult.Data = ring
		}
	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()

}


// @Title GetRingCateList
// @Description 服务类型列表
// @Param shopId  patch string   true string       "店鋪ID"
// @router /ringCateList [get]
func (c *RingApi) GetRingCateList() {

	var BaseResult = new(models.BaseResult)

	var shopId = c.GetString("shopId")

	if ( len(shopId) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "shopId不能为空"
	} else {

		var params = make(map[string]string)

		params["shop_id"] = shopId

		rings := dao.QeryRingCate(params)

		BaseResult.Data = rings
	}

	c.Data["json"] = BaseResult

	c.ServeJSON()

}



// @Title DelRingCate
// @Description 刪除类型服务
// @Param shopId  patch string   true string       "店鋪ID"
// @Param   ids     patch string   true int64       "服务类型ID列表，以'，'分割"
// @router /ringCateDel [get]
func (this *RingApi) DelRingCate() {

	var BaseResult = new(models.BaseResult)

	var idstr = this.GetString("ids")
	var shopId = this.GetString("shopId")

	if ( len(shopId) <= 0) {

		BaseResult.Code = 100
		BaseResult.Msg = "shopId不能为空"

	} else if ( len(idstr) <= 0) {

		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"

	} else {

		err := dao.DeleteRingCate(shopId, idstr)

		if (err != nil) {
			BaseResult.Code = 100
			BaseResult.Msg = err.Error()
		} else {
			BaseResult.Msg = "删除成功"
		}

	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()
}



// @Title AddRing
// @Description 新增服务类型
// @Param   cate     patch string   true string       "呼叫类型json"
// @router /addCate [get]
func (c *RingApi) AddRingCate() {

	var BaseResult = new(models.BaseResult)

	var cateStr = c.GetString("cate")

	if ( len(cateStr) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"
	} else {

		var mapStr map[string]interface{}

		json.Unmarshal([]byte(cateStr), &mapStr)

		for k := range mapStr {
			if k == "Id" {
				mapStr[k] = nil
			}
		}

		var cate = new(models.RingCate)

		mapstructure.Decode(mapStr, cate);

		if (cate != nil &&cate.Id >= 0) {
			if err := dao.InserRingCate(cate); (utils.CheckResult(BaseResult, err)) {
				BaseResult.Data = cate
			}
		} else {
			BaseResult.Code = 100
			BaseResult.Msg = "参数类型错误"
		}

	}
	c.Data["json"] = BaseResult

	c.ServeJSON()

}


// @Title RingUpdate
// @Description  服务类型修改
// @Param   cate     patch string   true string       "呼叫类型json"
// @router /ringCateUpdate [get]
func (this *RingApi) RingCateUpdate() {

	var BaseResult = new(models.BaseResult)
	var cate = new(models.RingCate)

	var cateJson = this.GetString("cate")

	if ( len(cateJson) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数不能为空"
	} else {

		json.Unmarshal([]byte(cateJson), &cate)

		err := dao.UpdateRingCate(cate)

		if (utils.CheckResult(BaseResult, err)) {
			BaseResult.Data = cate
		}
	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()

}






